// pages/mine/bankcard/edit/index.js
const service = require('../../../../service/other')
const util = require('../../../../utils/util')
Page({

    data: {
        form: {
            input: ''
        },
        pickerIndex: null,
        bankArray: ['工商银行', '建设银行', '招商银行'],
    },

    bindPickerChange(e) {
        let index = e.detail.value
        this.setData({
            pickerIndex: index
        })
    },

    toast(title) {
        wx.showToast({ title: title, icon: 'none' })
    },

    formSubmit(e) {
        console.log(111111, e)
        let param = e.detail.value
        let pickerIndex = this.data.pickerIndex

        if (pickerIndex === null) {
            return this.toast('请选择开户银行!')
        } else {
            param['cardBelongsBank'] = this.data.bankArray[pickerIndex]
        }

        if (!param.cardId) {
            return this.toast('请填写银行卡号')
        } else {
            let reg = /^([1-9]{1})(\d{15}|\d{18})$/;
            if (!reg.test(param.cardId)) {
                wx.showToast({
                    title: '银行卡格式不正确',
                    icon: 'none'
                })
                return
            }
        }

        if (!param.human) {
            return this.toast('请填写持卡人姓名')
        }

        if (!param.moble) {
            return this.toast('请填写持卡人手机号码')
        } else {
            if (!util.regPhone(param.moble)) {
                wx.showToast({
                    title: '手机号格式不正确!',
                    icon: 'none'
                })
                return
            }

        }

        if (!param.idCard) {
            return this.toast('请填写持卡人身份证号')
        } else {
            if (!util.regIdentity(param.idCard)) {
                wx.showToast({
                    title: '身份证格式不正确!',
                    icon: 'none'
                })
                return
            }
        }


        let userId = wx.getStorageSync("userInfo").id;
        param['customerId'] = userId
        service.addBack(param, (res) => {
            if (res.code == '00000') {
                wx.showToast({
                    title: '添加银行卡成功!',
                })
                wx.navigateBack({
                    delta: 1,
                })
            } else {
                wx.showToast({
                    title: res.message,
                    icon: 'none'
                })
            }
        })







    },
})